package com.atguigu.demojuc.chap08;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ExecutorsDemo_ {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //1.采用Executors产生线程池
        //1.1 创建单一线程-线程池对象
        ExecutorService executorService = Executors.newSingleThreadExecutor();//高并发情况下，一池一线程，线程不够用，大量请求等待处理

        //2.调用线程池对象执行任务(Runnable,Callable)
        //2.1 execute方法入参只能是Runnable，只适用线程没有返回结果
        //executorService.execute();
        //2.2 submit方法入参可以Runnable或则Callable，适用于线程没有返回结果
        //executorService.submit()

        Future<String> future =executorService.submit(()->{
                    System.out.println((Thread.currentThread().getName() + ",任务执行。。。"));
                    return "atguigu";
        });

        //3.获取子线程的执行结果
        String s = future.get();
        System.out.println((Thread.currentThread().getName() + "," + s));
        //3.测试时候-线程池使用完毕后关闭
//        executorService.shutdown();


    }
}
